package sanmubird.club.mock.minenv;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import sanmubird.club.mock.service.MockDemoService;
import sanmubird.club.mock.service.MockDependencyService;
import sanmubird.club.mock.service.MockNestDependencyService;
import sanmubird.club.mock.service.MockRemoteInvokeService;

/**
 * @author sam
 * @date 2019/12/8 12:05
 * @description 功能描述
 * <p>
 * 在前两个例子中 没有依赖 Spring 我们依赖 mock 和 反射 来实例化所需的对象
 * 这里将看 Spring 会怎么实现
 * 注意： 还需要 继承 AbstractJUnit4SpringContextTests 这个类  这个类的注解有 @RunWith(SpringRunner.class)
 * 需注意: 如果要启用 Spring和 Mockito，必须添加这么一行：@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
 */

@ContextConfiguration(classes = {MockDependencyService.class, MockNestDependencyService.class})
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class MockBeanAnnotationTest4Spring2 extends AbstractJUnit4SpringContextTests {

	@MockBean
	MockDemoService mockDemoService;

	@MockBean
	MockRemoteInvokeService mockRemoteInvokeService;

	@Autowired
	MockDependencyService mockDependencyService;

	@Autowired
	MockNestDependencyService mockNestDependencyService;

	@Test
	public void testInjectMocks() {
		Mockito.when(mockDemoService.simpleService()).thenReturn("mockDemoService");
		Mockito.when(mockRemoteInvokeService.remoteInvoke()).thenReturn("remoteInvoke");
		final String actual = mockNestDependencyService.dependencyDemo();
		Assert.assertEquals("mockDemoService remoteInvoke", actual);
	}


}
